#include "stack.h"
void STInit(ST* ps){
    assert(ps);
    ps->a=(STDataType*)malloc(sizeof (STDataType)*4);
    if (ps->a==NULL){
        printf("malloc fail\n");
    }
    ps->capacity=4;
    ps->top=0;
}
void Destory(ST* ps){
    assert(ps);
    free(ps->a);
    ps->a=NULL;
    ps->top=ps->capacity=0;
}
//链表顶部插入和删除，入栈出栈
void StackPush(ST* ps,STDataType x){
    assert(ps);
    if (ps->top==ps->capacity){
        STDataType* temp= (STDataType*)realloc(ps->a,ps->capacity*2*sizeof(STDataType));
        if (temp==NULL){
            printf("realloc fail\n");
        }else{
            ps->a=temp;
            ps->capacity*=2;
        }
    }
    ps->a[ps->top]=x;
    ps->top++;
}
void StackPop(ST* ps){
    assert(ps);
    //栈空了，直接报错
    assert(ps->top>0);
    ps->top--;
}
//获取栈顶值和栈的长度
STDataType StackTop(ST* ps){
    assert(ps);
    assert(ps->top>0);
    return ps->a[ps->top-1];
}
int StackSize(ST* ps){
    return ps->top;
}
bool StackEmpty(ST* ps){
    assert(ps);
    return  ps->top==0;
}